package com.sand.result;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class ResponseResult<T> implements Serializable {

    public Integer code;

    public String msg;

    public T data;

    public ResponseResult() {
        this.code = 200;
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public   ResponseResult<T> errorResult(int code, String msg) {
        ResponseResult<T> result = new ResponseResult<T>();
        return result.error(code, msg);
    }

    public  ResponseResult<T> okResult(int code, String msg) {
        ResponseResult<T> result = new ResponseResult<T>();
        return result.ok(code, null, msg);
    }

    public  ResponseResult<T> okResult(Object data) {
        ResponseResult<T> result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS , AppHttpCodeEnum.SUCCESS.getMsg());
        if(data!=null) {
            result.setData((T) data);
        }
        return result;
    }

    public  ResponseResult<T> okResult(Object data , String msg) {
        ResponseResult<T> result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS , msg);
        if(data!=null) {
            result.setData((T) data);
        }
        return result;
    }


    public  ResponseResult<T> errorResult(AppHttpCodeEnum enums){
        return setAppHttpCodeEnum(enums,enums.getMsg());
    }

    public  ResponseResult<T> errorResult(AppHttpCodeEnum enums, String errorMessage){
        return setAppHttpCodeEnum(enums,errorMessage);
    }

    public  ResponseResult<T> setAppHttpCodeEnum(AppHttpCodeEnum enums){
        return okResult(enums.getCode());
    }

    private  ResponseResult<T> setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg){
        return okResult(enums.getCode(),msg);
    }

    public ResponseResult<T> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResponseResult<T> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<T> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public ResponseResult<T> hint(String msg){
        this.code = 200;
        this.msg = msg;
        this.data = null;
        return this;
    }

    public ResponseResult<T> ok(T data) {
        this.data = data;
        return this;
    }


}
